package com.yonyou.pmclouds.enterprise.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.enterprise.entity.EnterpriseQueryParam;
import com.yonyou.pmclouds.enterprise.entity.EnterpriseRoleEnum;
import com.yonyou.pmclouds.enterprise.entity.EnterpriseVO;
import com.yonyou.pmclouds.enterprise.rmiitf.EnterpriseMaintain;
import com.yonyou.pmclouds.enterprise.rmiitf.EnterpriseQuery;
import com.yonyou.pmclouds.personnel.entity.PersonnelRole;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author chulf
 * @Date 2020/4/15
 * @Description 友工程企业档案信息接口
 **/
@RestController
@RequestMapping("/ygcenterprise")
public class YgcEnterpriseController {

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private EnterpriseQuery enterpriseQuery;

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private EnterpriseMaintain enterpriseMaintain;

    /**
     *  刷新及初始化用该方法
     * @return
     * @throws BusinessException
     */

    @RequestMapping("/list")
    public EnterpriseVO[] list() throws BusinessException {
        EnterpriseQueryParam param = new EnterpriseQueryParam();
        param.setTenantId(RuntimeEnvironment.getTenantId());

        return enterpriseQuery.queryEnterprisesByUser(param);
    }

    /**
     * 条件查询用该方法
     * @param param
     * @return
     * @throws BusinessException
     */
    @RequestMapping("/queryByCondition")
    @RequiresRoles(value = PersonnelRole.ENTERPRISE_ADMIN)
    public EnterpriseVO[] queryByCondition (@RequestBody EnterpriseQueryParam param) throws BusinessException {

        param.setTenantId(RuntimeEnvironment.getTenantId());

        return enterpriseQuery.queryEnterprisesByUser(param);
    }


    @RequestMapping("/queryDetail")
    @RequiresRoles(value = {PersonnelRole.ENTERPRISE_CONTACT,PersonnelRole.ENTERPRISE_ADMIN},logical = Logical.OR)
    public EnterpriseVO queryDetail(String pkEnterprise) throws BusinessException {

        EnterpriseVO enterpriseVO = enterpriseQuery.queryEnterpriseDetail(pkEnterprise);

        return enterpriseVO;
    }

    @PostMapping("/save")
    @RequiresRoles(value = {PersonnelRole.ENTERPRISE_CONTACT,PersonnelRole.ENTERPRISE_ADMIN},logical = Logical.OR)
    public EnterpriseVO save(@RequestBody EnterpriseVO enterpriseVO) throws BusinessException{

        enterpriseVO.setPkTenant(RuntimeEnvironment.getTenantId());
        return enterpriseMaintain.saveEnterpriseVO(enterpriseVO);
    }

    @PostMapping("/delete")
    @RequiresRoles(value = PersonnelRole.ENTERPRISE_ADMIN)
    public void delete(@RequestBody EnterpriseVO[] enterpriseVOS) throws BusinessException{

        enterpriseMaintain.deleteEnterpriseVOS(enterpriseVOS);
    }

    @RequestMapping("/addEtpByOrg")
    @RequiresRoles(value = PersonnelRole.ENTERPRISE_ADMIN)
    public EnterpriseVO addEtpByOrg(String pkOrg) throws BusinessException {
        EnterpriseVO enterpriseVO = enterpriseMaintain.addEnterpriseByOrg(pkOrg);

        return enterpriseVO;
    }
    @RequestMapping("/queryEtpsByRole")
    public EnterpriseVO[] queryEtpsByRole(byte role) throws BusinessException {

        EnterpriseVO[] enterpriseVOS = enterpriseQuery.queryEnterprisesByRole(role);

        return enterpriseVOS;
    }

    @RequestMapping("/add")
    @RequiresRoles(value = PersonnelRole.ENTERPRISE_ADMIN)
    public boolean add(){
        // 对应新增 如果没有异常证明有对应权限返回true
        return true;
    }

    @RequestMapping("/queryBySearchCondition")
    public EnterpriseVO[] queryBySearchCondition(@RequestBody EnterpriseQueryParam queryParam) throws BusinessException{
        return enterpriseQuery.queryByCondition(queryParam);
    }

    @RequiresRoles({"test"})
    @RequestMapping("/test")
    public void test(){
        System.out.println("test");
    }
}
